<?php if(!defined('IN_WDPHP'))exit('No direct script access.');
/**
 * WDPHP  			上传类
 * @package 		WDPHP
 * @author 			肖伟 
 * @version 		v1.0
 * @copyright 		Copyright (c) 2012. widuu.com
 */


	class Upload{
		//目标保存路径
		public $savePath='./Public/uploads/';
		//最大尺寸单位M
		public $maxSize=2;
		//允许的文件后缀
		public $allowSuffix=array('jpg','png','jpeg','bmp','gif');
		//是否启用Mime检测
		public $checkMime=false;
		//允许的MIME类型
		public $allowMime=array('image/jpeg','image/jpg','image/png','image/x-png','image/wbmp','image/gif');
		//是否启用随机文件名
		public $randName=true;
		//随机文件名前缀
		public $filePrefix;
		//保存的文件名
		protected $fileName;
		//消息记录
		protected $info;
		
		/**
		 * 构造函数
		 * @param string $savePath 文件存在路径
		 */
		public function __construct($savePath=''){
			if(empty($savePath)||!file_exists($savePath)){
				Debug::addmsg('文件保存路径未设置或不存在,采用默认路径:/Public/uploads/');
			}else{
				$this->savePath=rtrim($savePath.'/').'/';
			}
			if(!file_exists($this->savePath))
				mkdir($this->savePath,0777,true);
		}
		
		/**
		 * 上传文件
		 * @param unknown_type $fileName
		 */
		public function uploadFile($formName){
			if (empty($_FILES)) {
				Debug::addmsg('<span class="red">没有选择上传文件!</span>');
				return false;
			}
			$name = $_FILES[$formName]['name'];
			$type = $_FILES[$formName]['type'];
			$size = $_FILES[$formName]['size'];
			$tmpName=$_FILES[$formName]['tmp_name'];
			$error = $_FILES[$formName]['error'];
			//多文件上传
			if(is_array($name)){
				foreach($name  as $key=>$fname){
					if($error[$key]){
						$this->info[]='文件['.$fname.']错误:'.$this->info($error[$key]);
						continue;
					}
					//文件后缀
					$arr=explode('.', $fname);
					$suffix=array_pop($arr);
					//检测后缀
					if(!in_array($suffix, $this->allowSuffix)){
						Debug::addmsg('<span class="red">文件['.$fname.']的后缀'.$suffix.'不允许上传!</span>');
						continue;
					}
					//检测大小
					if(!$this->checkSize($size[$key])){
						Debug::addmsg('<span class="red">文件['.$fname.']的大小超过限制:'.$this->maxSize.'(单位:M)</span>');
						continue;
					}
					//检测MIME
					if($this->checkMime && !$this->checkMime($type[$key])){
						Debug::addmsg('<span class="red">文件['.$fname.']的Mime类型不允许上传!</span>');
						continue;
					}
					//获取保存文件名
					$fileSaveName=$this->getSaveName($fname);
					$this->fileName[$key]=$fileSaveName;
					//移动,保存相关信息
					if($this->save($tmpName[$key], $fileSaveName)){
						$this->info[]='文件['.$fname.']上传成功!';
					}else{
						$this->info[]='文件['.$fname.']上传失败!';
					}	
				}
			
				return $this->info;
			}else{//单文件上传
				if($error){
					$this->info[]='文件['.$fname.']错误:'.$this->error($error);
					return false;
				}
				//文件后缀
				$arr=explode('.', $name);
				$suffix=array_pop($arr);
				//检测后缀
				if(!in_array($suffix, $this->allowSuffix)){
					Debug::addmsg('<span class="red">文件['.$name.']的后缀'.$suffix.'不允许上传!</span>');
					return false;
				}
				//检测大小
				if(!$this->checkSize($size)){
					Debug::addmsg('<span class="red">文件['.$name.']的大小超过限制:'.$this->maxSize.'(单位:M)</span>');
					return false;
				}
				//检测MIME
				if($this->checkMime && !$this->checkMime($type)){
					Debug::addmsg('<span class="red">文件['.$name.']的Mime类型不允许上传!</span>');
					return false;
				}
				//获取保存文件名
				$fileSaveName=$this->getSaveName($name);
				$this->fileName=$fileSaveName;
				//移动,保存相关信息
				if($this->save($tmpName, $fileSaveName)){
					$this->info[]='文件['.$name.']上传成功!';
				}else{
					$this->info[]='文件['.$name.']上传失败!';
				}
			}
				
		}
		
		
		/**
		 * 保存文件
		 * @param string $tmp_name 临时文件名
		 * @param string $destination 目标路径
		 * @return boolean
		 */
		protected function save($tmp_name,$destination){
			return move_uploaded_file($tmp_name, $destination);
		}
		
		//设置文件名
		public function getSaveName($fileName){
			if($this->randName){//随机文件名:public/uploads/files/pre_2424551223.jpg
				$arr=explode('.',$fileName);
				return $this->savePath.$this->filePrefix.uniqid().'.'.array_pop($arr);
			}else{//不启用随机文件名:public/uploads/files/pre_a.jpg
				return $this->savePath.$this->filePrefix.$fileName;
			}
		}
		
		//检测尺寸
		protected function checkSize($size){
			return ($size/pow(1024,2))<$this->maxSize;
		}
		
		//检测Mime
		protected function checkMime($mime){
			return in_array($mime, $this->allowMime);
		}
		
		//检测后缀
		protected function checkSuffix($suffix){
			return in_array($suffix, $this->allowSuffix);
		}
		
		//设置属性
		public function __set($proName,$value){
			if(isset($this->$proName)){
				$this->$proName=$value;
			}else{ 
				Debug::addmsg('<span class="red">Upload类中不存在属性:'.$proName.'!</span>');
			}
		}
		
		//获取上传成功后的文件名
		public function __get($proName){
			if(strtolower($proName)=='filename')
				return $this->fileName;
			elseif(strtolower($proName)=='info')
				return $this->info;
		}
		
		/**
		 * 错误处理
		 * @param number $errorNo 错误号
		 */
		protected function error($errorNo){		
			switch($errorNo) {		
				case 1:		
					return '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';		
					break;		
				case 2:		
					return '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';		
					break;		
				case 3:		
					return '文件只有部分被上传';		
					break;		
				case 4:		
					return '没有文件被上传';		
					break;		
				case 6:		
					return '找不到临时文件夹';		
					break;		
				case 7:		
					return '文件写入失败';		
					break;
				default:	
					return '未知上传错误！';	
			}		
		}
		
	}